//
//  BN_PersonListData.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-12.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_PersonListData.h"

@implementation BN_PersonListData {
    BN_ChatHelper *chatHelper_;
}

- (id)init {
    if (self = [super init]) {

        if (!chatHelper_) {
            chatHelper_ = [[BN_ChatHelper alloc]init];
        }
        
        if (!self.netArray) {
            self.netArray = [[NSMutableArray alloc]init];
        }
        
        if (!self.teacherArray) {
            self.teacherArray = [[NSMutableArray alloc]init];
        }
        
        if (!self.parentArray) {
            self.parentArray = [[NSMutableArray alloc]init];
        }
        
        [self loadNetData];
    }
    return self;
}

- (void)SelectPersonInfo:(Chat_SelectPersonInfo)chatSelePerson {
    self.chatSelectPersonInfo = chatSelePerson;
}

- (void)loadNetData {
    
    __weak typeof(self) weakSelf = self;
    [chatHelper_ queryChatPersonList:^(id result, NSString *errMsg) {
        // update by jason yan 2014-05-30
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            weakSelf.chatSelectPersonInfo(YES);
        }
        if(result){
            [weakSelf dealNetArray:result];
            weakSelf.chatSelectPersonInfo(YES);
        }
    }];
}

- (void)dealNetArray:(id)info {
    NSDictionary *receiveDict = (NSDictionary *)info;
    self.teacherArray = receiveDict[@"techInfo"];
    self.parentArray = receiveDict[@"pareInfo"];
    [self.selectPersonTableView reloadData];
}
#pragma mark
#pragma mark Action

- (void)lookPersonInfo:(id)result {
    [[NSNotificationCenter defaultCenter] postNotificationName:K_CHAT_SELECTPERSON_LOOK_PERSONINFO object:nil];
}

#pragma mark
#pragma mark 列表委托和数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger sectionCount = 0;
    
    if (section == 0) {
        sectionCount = self.teacherArray.count;
    }
    if (section == 1) {
        sectionCount = self.parentArray.count;
    }
    return sectionCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    // cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
    cell.textLabel.font = [UIFont systemFontOfSize:15];

    NSString *tagStr;
    
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = self.teacherArray[indexPath.row][@"userName"];
            tagStr = self.teacherArray[indexPath.row][@"userId"];
            cell.tag = [tagStr integerValue];
            break;
        case 1:
            cell.textLabel.text = self.parentArray[indexPath.row][@"userName"];
            tagStr = self.parentArray[indexPath.row][@"userId"];
            cell.tag = [tagStr integerValue];
            break;
        default:
            break;
    }
    
    cell.imageView.image = [UIImage imageNamed:@"chat_userPhoto"];
    // cell.imageView.userInteractionEnabled = YES;
    // UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookPersonInfo:)];
    // [cell.imageView addGestureRecognizer:singleTap];
    return cell;
}

/* - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = nil;
            if (self.parentArray.count > 0 && self.teacherArray.count > 0) {
                switch (section) {
                    case 0:
                        sectionTitle = @"    老师";
                        break;
                    case 1:
                        sectionTitle = @"    家长";
                        break;
                    default:
                        break;
                }
            }else {
                return nil;
            }
    return sectionTitle;
}*/

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    UILabel *headerTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, (20-16)/2, 100, 16)];
    headerTitleLabel.backgroundColor = [UIColor clearColor];
    headerTitleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16];
    if (section == 0) {
        headerTitleLabel.text = @"老 师";
    }else {
        headerTitleLabel.text = @"家 长";
    }
    headerView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [headerView addSubview:headerTitleLabel];
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.userId = [NSString stringWithFormat:@"%ld",(long)cell.tag];
    
    NSDictionary *dict = [[NSDictionary alloc]init];
    dict = @{@"userId": self.userId,@"userName":cell.textLabel.text};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"KCHAT_PUSHFACE" object:dict];
}

#pragma mark
#pragma mark 生命周期
- (void)dealloc
{
    NSLog(@"聊聊联系人Modal释放...");
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KCHAT_PUSHFACE" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:K_CHAT_SELECTPERSON_LOOK_PERSONINFO object:nil];
}
/*
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    headerView.backgroundColor = [UIColor grayColor];
    return headerView;
}*/

@end
